package com.gzu.finalwork.admin.service;

import com.gzu.finalwork.admin.mapper.AdminDashboardMapper;
import com.gzu.finalwork.common.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
public class AdminDashboardService {

    @Autowired
    private AdminDashboardMapper adminDashboardMapper;

    /**
     * 获取仪表盘完整数据
     * @return 仪表盘数据
     */
    public Object getDashboardData() {
        try {
            Map<String, Object> dashboardData = new HashMap<>();
            
            // 总订单数及增长率
            int totalOrders = adminDashboardMapper.getTotalOrders();
            double totalOrdersGrowth = adminDashboardMapper.getTotalOrdersGrowthRate();
            Map<String, Object> totalOrdersData = new HashMap<>();
            totalOrdersData.put("count", totalOrders);
            totalOrdersData.put("growth", totalOrdersGrowth);
            totalOrdersData.put("isUp", totalOrdersGrowth >= 0);
            dashboardData.put("totalOrders", totalOrdersData);
            
            // 运输中订单数及增长率
            int inTransitOrders = adminDashboardMapper.getInTransitOrders();
            double inTransitGrowth = adminDashboardMapper.getInTransitOrdersGrowthRate();
            Map<String, Object> inTransitData = new HashMap<>();
            inTransitData.put("count", inTransitOrders);
            inTransitData.put("growth", inTransitGrowth);
            inTransitData.put("isUp", inTransitGrowth >= 0);
            dashboardData.put("inTransitOrders", inTransitData);
            
            // 今日签收数及增长率
            int todayDelivered = adminDashboardMapper.getTodayDeliveredOrders();
            double deliveredGrowth = adminDashboardMapper.getTodayDeliveredGrowthRate();
            Map<String, Object> deliveredData = new HashMap<>();
            deliveredData.put("count", todayDelivered);
            deliveredData.put("growth", deliveredGrowth);
            deliveredData.put("isUp", deliveredGrowth >= 0);
            dashboardData.put("todayDelivered", deliveredData);
            
            // 今日收入及增长率
            BigDecimal todayRevenue = adminDashboardMapper.getTodayRevenue();
            double revenueGrowth = adminDashboardMapper.getTodayRevenueGrowthRate();
            Map<String, Object> revenueData = new HashMap<>();
            revenueData.put("amount", todayRevenue);
            revenueData.put("growth", revenueGrowth);
            revenueData.put("isUp", revenueGrowth >= 0);
            dashboardData.put("todayRevenue", revenueData);
            
            // 今日新订单数
            int todayNewOrders = adminDashboardMapper.getTodayNewOrders();
            dashboardData.put("todayNewOrders", todayNewOrders);
            
            // 系统运行状态
            dashboardData.put("systemStatus", "正常");
            
            return AjaxResult.success("获取仪表盘数据成功", dashboardData);
        } catch (Exception e) {
            return AjaxResult.error(500, "获取仪表盘数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取今日新订单数
     * @return 今日新订单数
     */
    public Object getTodayOrders() {
        try {
            int todayNewOrders = adminDashboardMapper.getTodayNewOrders();
            Map<String, Object> data = new HashMap<>();
            data.put("count", todayNewOrders);
            data.put("message", "今日共有" + todayNewOrders + "个新订单");
            
            return AjaxResult.success("获取今日订单数成功", data);
        } catch (Exception e) {
            return AjaxResult.error(500, "获取今日订单数失败：" + e.getMessage());
        }
    }

    /**
     * 获取订单统计数据
     * @return 订单统计
     */
    public Object getOrderStats() {
        try {
            Map<String, Object> stats = new HashMap<>();
            
            // 各状态订单统计
            stats.put("waitingPickup", adminDashboardMapper.getOrdersByStatus(0));      // 待揽收
            stats.put("waitingShipment", adminDashboardMapper.getOrdersByStatus(1));    // 待发货
            stats.put("inTransit", adminDashboardMapper.getOrdersByStatus(2));          // 运输中
            stats.put("waitingDelivery", adminDashboardMapper.getOrdersByStatus(3));    // 待签收
            stats.put("completed", adminDashboardMapper.getOrdersByStatus(4));          // 已完成
            
            // 时间段统计
            stats.put("todayOrders", adminDashboardMapper.getTodayNewOrders());
            stats.put("weekOrders", adminDashboardMapper.getWeekOrders());
            stats.put("monthOrders", adminDashboardMapper.getMonthOrders());
            
            return AjaxResult.success("获取订单统计成功", stats);
        } catch (Exception e) {
            return AjaxResult.error(500, "获取订单统计失败：" + e.getMessage());
        }
    }

    /**
     * 获取收入统计数据
     * @return 收入统计
     */
    public Object getRevenueStats() {
        try {
            Map<String, Object> stats = new HashMap<>();
            
            // 收入统计
            stats.put("todayRevenue", adminDashboardMapper.getTodayRevenue());
            stats.put("weekRevenue", adminDashboardMapper.getWeekRevenue());
            stats.put("monthRevenue", adminDashboardMapper.getMonthRevenue());
            stats.put("totalRevenue", adminDashboardMapper.getTotalRevenue());
            
            // 增长率统计
            stats.put("todayGrowth", adminDashboardMapper.getTodayRevenueGrowthRate());
            stats.put("weekGrowth", adminDashboardMapper.getWeekRevenueGrowthRate());
            stats.put("monthGrowth", adminDashboardMapper.getMonthRevenueGrowthRate());
            
            return AjaxResult.success("获取收入统计成功", stats);
        } catch (Exception e) {
            return AjaxResult.error(500, "获取收入统计失败：" + e.getMessage());
        }
    }
} 